<template>
  <a-card title="体格检查" :bordered="false" class="card-area">
    <a-spin :spinning="loading">
      <a-form-model
        :model="model"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
        labelAlign="left"
        :colon="false"
      >
        <a-tabs @change="callback" v-model="active">
          <a-tab-pane key="1" tab="基础检查">
            <BasicChecking :physical="model" />
          </a-tab-pane>
          <a-tab-pane key="2" tab="眼底检查">
            <FundusChecking :physical="model" />
          </a-tab-pane>
          <a-tab-pane key="3" tab="皮肤检查">
            <SkinChecking :physical="model" />
          </a-tab-pane>
          <a-tab-pane key="4" tab="糖足检查">
            <SugarFootChecking :physical="model" />
          </a-tab-pane>
          <a-tab-pane key="5" tab="其它">
            <OtherChecking :physical="model" />
          </a-tab-pane>
        </a-tabs>

        <a-form-model-item :wrapper-col="{ span: 12, offset: 10 }">
          <a-button type="primary" @click="onSave" :loading="saveLoading">保存</a-button>
          <a-popconfirm placement="topLeft" ok-text="是" cancel-text="否" @confirm="doConfirm">
            <template slot="title">
              <p>{{ text }}</p>
            </template>
            <a-button type="primary" style="margin-left: 30px" :loading="confirmLoading">确认完成</a-button>
          </a-popconfirm>
          <a-button type="primary" style="margin-left: 30px" @click="endServiceOk" :loading="saveLoading"
            >服务后数据录入完成</a-button
          >
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-card>
</template>

<script>
import { PAGE_MENU_TYPE } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'
import BasicChecking from './physicalinfo/BasicChecking'
import FundusChecking from './physicalinfo/FundusChecking'
import SkinChecking from './physicalinfo/SkinChecking'
import SugarFootChecking from './physicalinfo/SugarFootChecking'
import OtherChecking from './physicalinfo/OtherChecking'
import bus from '@/utils/bus'

export default {
  name: 'Physical',
  inject: ['closeCurrent'],
  components: { BasicChecking, FundusChecking, SkinChecking, SugarFootChecking, OtherChecking },
  data() {
    return {
      loading: true,
      saveLoading: false,
      confirmLoading: false,
      model: {},
      type: '1',
      active: '1',
      text: '此操作代表你已完成了页面的填写，是否继续？',
      formItemLayout: {
        labelCol: {
          xs: { span: 12 },
          sm: { span: 2 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        }
      },
      tailFormItemLayout: {
        wrapperCol: {
          xs: {
            span: 24,
            offset: 0
          },
          sm: {
            span: 12,
            offset: 2
          }
        }
      }
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    getInfo() {
      let patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]
      getAction('102/11/1021102', {
        patientId: patientId
      })
        .then(res => {
          if (res.success) {
            this.model = res.result
          } else {
            postAction('102/11/1021101', {
              patientId: patientId
            }).then(newRes => {
              if (newRes.success) {
                this.getInfo()
              } else {
                this.$message.error('患者档案创建失败')
              }
            })
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    onSave() {
      this.loading = true
      this.saveLoading = true
      let queryData = JSON.parse(JSON.stringify(this.model))
      postAction('102/11/1021101', queryData)
        .then(res => {
          if (res.success) {
            this.$message.success('操作成功')
            this.active != '5' && (this.active = JSON.stringify(Number(this.active) + 1))
          } else {
            this.$message.error('操作失败')
          }
        })
        .finally(_ => {
          this.saveLoading = false
          this.loading = false
        })
    },
    doConfirm() {
      this.confirmLoading = true

      let patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]
      let params = {
        patientId: patientId,
        isFinished: 1,
        type: this.type
      }
      postAction('102/10/1021103', params)
        .then(res => {
          if (res.success) {
            this.$message.success('页面已确认', 5)
            this.active != '5' && (this.active = JSON.stringify(Number(this.active) + 1))
          }
        })
        .finally(_ => {
          this.confirmLoading = false
        })
    },
    callback(key) {
      this.type = key
    },
    endServiceOk() {
      this.onSave()
      bus.$emit('modifiedDataForFinalReportByPhysical', {
        type: '体格检查',
        data: this.model
      })
      // this.$router.back(-1)
    }
  }
}
</script>

<style lang="less" scoped>
.ant-form-item {
  margin: 0.5rem;
}
</style>

